<?php
require_once('inc/init.php');
if(isLogin()){
	//_header_("location:index.php");
}
require_once getCacheFilePath('forums.php');

$action=strFilter($_GET['action']);
$username=strFilter($_POST['username']);
$userpass=strFilter($_POST['userpass']);
$userrem=strFilter($_POST['userrem']);
$securitycode=strFilter($_POST['securitycode']);
$userhidden=numFilter($_POST['userhidden']);
$loginaction="login.php?action=login";
$headtitle="会员登录";

if($action=='login'){
	if($username==''||$userpass==''){
		printMsg('login_detailsrequired');
	}
	if($cache_settings['loginsecuritycode']=='1' && strtolower($_SESSION['validationcode'])!=strtolower($securitycode)){
		printMsg('login_validationcodeerr');
	}
	$userpass_encrypt = encrypt($username,$userpass);
	$row=$db->row_select_one("users","username='{$username}' and userpass='{$userpass_encrypt}'","id,username,groupid,logintime,postsnum,credits1,credits2,credits3,credits4,credits5,credits6,credits7,credits8");
	require_once('loginext.php');
	if(empty($row)){
		printMsg('login_namepasserr');
	}else{
		if($row['groupid']==GROUP_NOVERIFY){	//待验证
			printMsg('login_group'.GROUP_NOVERIFY);
		}elseif($row['groupid']==GROUP_NOVISIT){	//禁止访问
			printMsg('login_group'.GROUP_NOVISIT);
		}elseif($row['groupid']==GROUP_VERIFYFAILED){	//验证失败
			printMsg('login_group'.GROUP_VERIFYFAILED);
		}else{
			if(numFilter($row['groupid'])<=80){
				$allmark=getAllCredits($cache_settings['creditsexpression'], $row);
				$row_group=$db->row_select_one("groups","({$allmark} BETWEEN marklower AND markhigher) and (groupid BETWEEN 1 AND 80)","groupid");
				if(empty($row_group)){
					printMsg('login_nogroup');
				}else{
					$row['groupid']=$row_group['groupid'];
				}
			}
			
			//更新登录时间
			$newrow["lastlogintime"] = $row["logintime"];
			$newrow["logintime"] = time();
			$newrow['groupid'] = $row['groupid'];
			$db->row_update("users",$newrow,"id={$row['id']}");
			
			//更新在线数据
			$db->row_delete("online","ip='".getIP()."'");
			
			$t=0;
			if($userrem=="y"){
				$t=3600*24*365;
			}
			$_SESSION['userid'] = $row['id'];
			$_SESSION['groupid'] = $row['groupid'];
			setCookies('username', $row['username'], $t);
			setCookies('userpass', $userpass_encrypt, $t);
			setCookies('userauth', md5($row['username'].$userpass_encrypt.$cache_settings['salt']), $t);
			setCookies('userhidden', $userhidden, $t);
			setCookies('expire', $t, $t);
			printMsg('login_succeed');
		}
	}
}else{
	$ref = $_GET["ref"];
	if($ref==""){
		$ref="index.php";
	}
	require_once('header.php');
	require_once getTemplatePath('login.htm');
	footer();
}
?>